package com.wyb.controller.goods;


import com.wyb.entity.Goods;
import com.wyb.service.impl.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/goods/addGood")
public class AddGoodsServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      //配置编码集过滤器后，不需要再单独配置
        req.setCharacterEncoding("utf-8");
      resp.setCharacterEncoding("utf-8");
        String goodsName = req.getParameter("goodsName");
        int stock = Integer.parseInt(req.getParameter("stock"));
        double costPrice = Double.parseDouble(req.getParameter("costPrice"));
        double retailPrice =Double.parseDouble(req.getParameter("retailPrice"));
        double discount = Double.parseDouble(req.getParameter("discount"));
        int status = Integer.parseInt(req.getParameter("status"));
        int categoryId = Integer.parseInt(req.getParameter("categoryId"));
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date listTime = null;
        try {
             listTime = sdf.parse(req.getParameter("listTime"));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        String introduction = req.getParameter("introduction");
        PrintWriter pw = resp.getWriter();
        boolean b=new GoodsServiceImpl().addGoods(new Goods(null,goodsName,stock,costPrice,retailPrice,discount,status,categoryId,listTime,null,null,introduction));
        pw.println(b);
        pw.flush();


    }
}
